/**
 * 
 */
package com.hexacta.hrs.web.struts.actions;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.vectrics.cv.ExtraCertificate;
import org.vectrics.user.Person;

import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.person.PersonService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author gvilla
 * 
 */
public class EditExtraCertificatesAction extends ActionSupport {

    private static final String LIST = "list";

    private String userId;

    private Long personId;

    private List<ExtraCertificate> certificates = new ArrayList<ExtraCertificate>();

    private String codes;

    private String names;

    private EmployeeService employeeService;

    private PersonService personService;

    public String editExtraCertificates() {

        Person person = getPerson();

        certificates.addAll(person.getCertificates());

        return SUCCESS;
    }

    public String saveAndAddNew() {
        if (!hasError()) {
            addCertificateToPerson();
        }
        return editExtraCertificates();
    }

    public String save() {
        if (hasError())
            return editExtraCertificates();
        addCertificateToPerson();
        return LIST;
    }

    public String cancel() {
        return LIST;
    }

    /**
     * 
     */
    private void addCertificateToPerson() {
        Person person = getPerson();

        if (!StringUtils.isEmpty(codes) && !StringUtils.isEmpty(names)) {
            person.getCertificates().add(new ExtraCertificate(codes, names));
            personService.save(person);
        }
    }

    /**
     * @return
     */
    private Person getPerson() {
        Person person = personService.getPersonById(personId);
        return person;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @return the personId
     */
    public Long getPersonId() {
        return personId;
    }

    /**
     * @param userId
     *            the userId to set
     */
    public void setUserId(final String userId) {
        this.userId = userId;
    }

    /**
     * @param personId
     *            the personId to set
     */
    public void setPersonId(final Long personId) {
        this.personId = personId;
    }

    /**
     * @return the employeeService
     */
    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    /**
     * @param employeeService
     *            the employeeService to set
     */
    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public String getFullName() {
        return getPerson().getName();
    }

    /**
     * @return the personService
     */
    public PersonService getPersonService() {
        return personService;
    }

    /**
     * @param personService
     *            the personService to set
     */
    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    /**
     * @return the certificates
     */
    public List<ExtraCertificate> getCertificates() {
        return certificates;
    }

    /**
     * @param certificates
     *            the certificates to set
     */
    public void setCertificates(final List<ExtraCertificate> certificates) {
        this.certificates = certificates;
    }

    /**
     * @return the codes
     */
    public String getCodes() {
        return codes;
    }

    /**
     * @return the names
     */
    public String getNames() {
        return names;
    }

    /**
     * @param codes
     *            the codes to set
     */
    public void setCodes(final String codes) {
        this.codes = codes;
    }

    /**
     * @param names
     *            the names to set
     */
    public void setNames(final String names) {
        this.names = names;
    }

    public boolean hasError() {

        boolean hasError = false;

        if (StringUtils.isEmpty(codes)) {
            addFieldError("codes1", "Code is required");
            hasError = true;
        }

        if (StringUtils.isEmpty(names)) {
            addFieldError("names1", "Name is required");
            hasError = true;
        }

        if (codes != null && codes.contains(" - ")) {
            addFieldError("codes2", "Code cannot contain the sequence \" - \"");
            hasError = true;
        }

        if (names != null && names.contains(" - ")) {
            addFieldError("names2", "Name cannot contain the sequence \" - \"");
            hasError = true;
        }

        return hasError;
    }
}
